package com.project.lx.text.application;

import android.app.Application;
import android.content.Context;


import com.project.lx.text.bean.User;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Meteor
 * @description: 自定义的Application类
 * @version:
 * @date: 2016/12/28 0028 15:50
 * @company: jijiaxuan.com
 * @email: lx802315@163.com
 */
public class MyApplication extends Application{

    //定义一个标记
    private static String TAG;

    //当前的用户
    private static User currentUser;

    /**
     * 维护一个全局的context对象
     */
    public Context context;

    //用于存放数据
    private static Map<String,Object> datas = new HashMap<>();
    //单例模式
    private static MyApplication myApplication = null;

    public static MyApplication getInstance() {
        return myApplication;
    }

    /**
     * 获取当前的用户对象
     *
     * @return
     */
    public User getCurrentUser() {
        User user = currentUser;
        if (user != null) {
            return user;
        }
        return null;
    }

    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }

    public static Object getDatas(String key, boolean delFlag) {
        if (delFlag) {
            return datas.remove(key);
        }
        return datas.get(key);
    }

    public static Object putDatas(String key, Object value) {
        return datas.put(key, value);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        TAG = this.getClass().getSimpleName();
        //由于Application类本身已经单例，所以直接按以下处理即可。
        myApplication = this;
        context = getApplicationContext();
    }
}
